package it.tic.sbibm.persistence.config;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;

public class SbConfiguration {

	private static Map<String, String> applicationParamsMap;

	  private static ResourceBundle applicationParamsResource =
			ResourceBundle.getBundle("it.tic.sbibm.persistence.config.sb_configuration");

	public static synchronized String getProperty(String name) {
		return SbConfiguration.getApplicationParams().get(name);
	}

	public static Map<String, String> getApplicationParams() {
		if (applicationParamsMap == null) {
			applicationParamsMap = new HashMap<String, String>();
			Enumeration<String> keys = applicationParamsResource.getKeys();
			while (keys.hasMoreElements()) {
				String key = keys.nextElement();
				String value = applicationParamsResource.getString(key);
				applicationParamsMap.put(key, value);
			}
		}
		return applicationParamsMap;
	}

   /**
	* Retrieves from properties file the description of a codified value
	* The method is synchronized because it can be invoked in multi-thread context 
	* 
	* @param propertiesGroug
	* @param specificValue
	* @return
	*/
	public static synchronized String getEncodingValue(String propertiesGroug, String specificValue) {
		String propertyName = propertiesGroug + "." + specificValue;
		return SbConfiguration.getApplicationParams().get(propertyName);
	}
	
	public static synchronized Map<String, String> getListOfValues(String commonKey) {

		Map<String, String> lista = new TreeMap<String, String>();
		Iterator<String> listaChiavi = SbConfiguration.getApplicationParams().keySet().iterator();
		while (listaChiavi.hasNext()) {
			String chiaveCompleta = listaChiavi.next();
			if (chiaveCompleta.contains(commonKey) ) {
				int inizio = commonKey.length() + 1;
				String chiave = chiaveCompleta.substring(inizio);
				String valore = SbConfiguration.getProperty(chiaveCompleta);
				lista.put(chiave, valore);
			}
		}
		return lista;
	}
}
